package com.wheel.logging.mdc.interceptor;/*
 * <h3>Title:wheel-service-ant系统</h3>
 * <p>Copyright (c) 2021, wnfbx@163.com. All rights reserved.</p>
 * @author wnfbx@163.com
 */

import com.wheel.core.utils.Log;
import com.wheel.core.utils.SnowFlakeID;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <dt>Description:MDCSetRequestIDFilter</dt>
 * <dd>设置MDC requestID</dd>
 *
 * @author wnfbx@163.com
 * @version 1.0 <i>2021/2/2 22:19</i>
 * @since jdk1.7+
 */
public class LoggingInterceptor extends HandlerInterceptorAdapter {

    private SnowFlakeID snowFlakeID;

    public LoggingInterceptor(SnowFlakeID snowFlakeID) {
        this.snowFlakeID = snowFlakeID;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        MDC.put(Log.RequestId, String.valueOf(snowFlakeID.nextId()));
        return super.preHandle(request, response, handler);
    }
}
